package com.zy.algorithm;

import com.zy.algorithm.bean.CartoonBuilderFactory;
import com.zy.algorithm.bean.SortStepBean;
import com.zy.algorithm.curtain.BaseSortScreenBean;
import com.zy.algorithm.curtain.IScreenBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CommonUtils {


    public static List<Integer> getIndexList(int left, int right) {
        List<Integer> indexList = new ArrayList<>();
        for (int i = left; i <= right; i++) {
            indexList.add(i);
        }
        return indexList;
    }

    //根据下标找获取子分组
    public static int[] getSubsetInts(int[] sort, List<Integer> subSetIndexList) {
        int[] subset = new int[subSetIndexList.size()];
        for (int i = 0; i < subSetIndexList.size(); i++) {
            subset[i] = sort[subSetIndexList.get(i)];
        }
        return subset;
    }


    //子分组复原到整个数组
    public static void dataSetBack(int[] sort, int[] subsetInts, List<Integer> subSetIndexList) {
        if (subSetIndexList.size() != subsetInts.length) {
            try {
                throw new Exception("数据不合法");
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }

        for (int i = 0; i < subsetInts.length; i++) {
            sort[subSetIndexList.get(i)] = subsetInts[i];
        }
    }

    public static List<IScreenBean> buildSingleScreen(int[] array, CartoonBuilderFactory builder) {
        BaseSortScreenBean onlySortScreenBean = new BaseSortScreenBean();
        onlySortScreenBean.setSortScreenStart(Arrays.copyOf(array, array.length));
        onlySortScreenBean.setSortedScreenStart(new ArrayList<>());

        List<SortStepBean> stepList = builder.build(array);

        onlySortScreenBean.setSortScreenEnd(Arrays.copyOf(array, array.length));
        onlySortScreenBean.setCartoonStepBeanList(stepList);
        //已排序OK
        List<Integer> sorted = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            sorted.add(i);
        }
        onlySortScreenBean.setSortedScreenEnd(sorted);

        List<IScreenBean> screenBeanList = new ArrayList<>();
        screenBeanList.add(onlySortScreenBean);
        return screenBeanList;
    }


}
